﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-11-20 13:29:27Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;

using Outcoder.UI.Xaml.Controls.Design;

using Microsoft.Phone.Shell;

namespace Outcoder.UI.Xaml.Controls
{
    public abstract class AppBarMenuItemBase : FrameworkElement, IAppBarItem,
        IApplicationBarMenuItem, IApplicationBarMenuItemProvider
    {
        public ApplicationBarMenuItem MenuItem { get; private set; }

        protected AppBarMenuItemBase()
        {
            MenuItem = new ApplicationBarMenuItem { Text = "Text" };

	        Loaded += HandleLoaded;
			Unloaded += HandleUnloaded;
        }

	    void HandleLoaded(object sender, RoutedEventArgs e)
	    {
			MenuItem.Click -= ApplicationBarMenuItemClick;
			MenuItem.Click += ApplicationBarMenuItemClick;
	    }

	    void HandleUnloaded(object sender, RoutedEventArgs e)
	    {
			MenuItem.Click -= ApplicationBarMenuItemClick;
	    }

	    public event EventHandler Click;

        public static readonly DependencyProperty IsEnabledProperty
            = DependencyProperty.RegisterAttached(
                "IsEnabled",
                typeof(bool),
                typeof(AppBarMenuItemBase),
                new PropertyMetadata(true, HandleEnabledChanged));

        static void HandleEnabledChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
               ((AppBarMenuItemBase)d).MenuItem.IsEnabled = (bool)e.NewValue;
        }

        public bool IsEnabled
        {
            get
            {
                return (bool)GetValue(IsEnabledProperty);
            }
            set
            {
                SetValue(IsEnabledProperty, value);
            }
        }

        public static readonly DependencyProperty TextProperty
            = DependencyProperty.RegisterAttached(
                "Text",
                typeof(string),
                typeof(AppBarMenuItemBase),
                new PropertyMetadata(HandleTextChanged));

        static void HandleTextChanged(DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != e.OldValue)
            {
                ((AppBarMenuItemBase)d).MenuItem.Text
                    = e.NewValue.ToString();
            }
        }

        void ApplicationBarMenuItemClick(object sender, EventArgs e)
        {
            OnClick(e);
        }

        protected virtual void OnClick(EventArgs e)
        {
            var tempEvent = Click;
            if (tempEvent != null)
            {
                tempEvent(this, e);
            }
        }

        void IAppBarItem.PerformClick()
        {
            if (!IsEnabled)
            {
                throw new InvalidOperationException("Element not enabled.");
            }
            ApplicationBarMenuItemClick(this, EventArgs.Empty);
        }

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }
    }
}